import { createRouter, createWebHistory } from 'vue-router'


const routes = [
  {
    path: '/',
    redirect:'/login',
   
  }
  ,
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    meta: {
      title: '登录'
    }
  }
  ,
  {
    path: '/bloghome',
    name: 'bloghome',
    component: () => import('@/views/BlogHome.vue'),
    meta: {
      title: '主页'
    }
  }
  ,
  {
    path: '/sort',
    name: 'sort',
    component: () => import('@/views/Sort.vue'),
    meta: {
      title: '分类'
    }
  }
  ,
  {
    path: '/discover',
    name: 'discover',
    component: () => import('@/views/Discover.vue'),
    meta: {
      title: '发现'
    }
  }
  ,
  {
    path: '/userinterface',
    name: 'userinterface',
    component: () => import('@/views/userInterface.vue'),
    meta: {
      title: '个人'
    }
  }
  ,
  {
    path: '/changeUserInformation',
    name: 'changeUserInformation',
    component: () => import('@/views/ChangeUserInformation.vue'),
    meta: {
      title: '修改界面'
    }
  }
  ,
  {
    path: '/blogContentDetail',
    name: 'blogContentDetail',
    component: () => import('@/views/BlogContentDetail.vue'),
    meta: {
      title: '文章内容'
    }
  }
  ,
  {
    path: '/blogPublish',
    name: 'blogPublish',
    component: () => import('@/views/BlogPublish.vue'),
    meta: {
      title: '文章发布'
    }
  },
  {
    path: '/ownArticle',
    name: 'ownArticle',
    component: () => import('@/views/ownArticle.vue'),
    meta: {
      title: '拙作'
    }
  },
   {
    path: '/person',
    name: 'person',
    component: () => import('@/views/PersonalHomepage.vue'),
    meta: {
      title: ''
    }
  }
  ,
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
//是一个在 Vue.js 中使用的路由守卫。它允许你在路由切换之前执行一些操作，例如验证用户身份、设置全局数据等。
//to:即将进入的目标路由对象。
//from：当前导航正要离开的路由。
//next：函数，必须要调用以结束钩子。可以传递参数给 next() 函数，这些参数将会被传递给目标路由的 beforeEnter 守卫。

const whiteList = ['/login']



router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  if (!whiteList.includes(to.path)) {
    if (!JSON.parse(sessionStorage.getItem('useInfo'))) {
      router.push('/login')
      return
    }
    next()
    return
  }
  next()
})



export default router
